﻿using System;
using System.Configuration;
using System.Web.Security;
using System.Web.UI;
using Medianamik.Core.Configuration;
using Medianamik.Core;

namespace Medianamik.UI.MasterPages
{
    /// <summary>
    /// Page "master" de Médianamik
    /// </summary>
	public partial class MedianamikMaster : BaseMasterPage
	{
        protected static readonly string ClientLogoPath = ((MedianamikConfigSection)ConfigurationManager.GetSection(ConfigurationSectionName.MEDIANAMIK)).ClientLogoPath;

		protected void Page_Load(object sender, EventArgs e)
		{
			cmdLogOut.Click += new ImageClickEventHandler(cmdLogOut_Click);
			ScriptManager.RegisterStartupScript(Page, Page.GetType(), "LayoutScript", "documentReady();", true);
            string title = Page.CurrentPage.DisplayName;
            Page.Title = string.Empty;
            if (!string.IsNullOrEmpty(title))
                Page.Title = title + " - ";
            Page.Title += "Medianamik";
            //if (Session["docked"] != null && ((string)Session["docked"]) == "true")
            //    RightPanelZone.DockedPaneId = "RightPanel";

		}

		/// <summary>
		/// Logout de Médianamik. Retourner à la page de login.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		void cmdLogOut_Click(object sender, ImageClickEventArgs e)
		{
			FormsAuthentication.SignOut();
            Session.Clear();
            Session.Abandon(); 
			Response.Redirect("/Medianamik/Login.aspx");
		}

		/// <summary>
		/// Fermer le panneau droit
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		public void cmdOpenClose_Click(object sender, EventArgs e)
		{
			//TODO: Conserver cette config
			//panRight.Visible = !panRight.Visible;
		}

        private int? _authentificationTimeout = null;
        public int AuthentificationTimeout
        {
            get
            {
                if (_authentificationTimeout.HasValue)
                    return _authentificationTimeout.Value;

                try
                {
                    System.Xml.XmlDocument x = new System.Xml.XmlDocument();
                    x.Load(Request.PhysicalApplicationPath + "web.config");
                    System.Xml.XmlNode node = x.SelectSingleNode("/configuration/system.web/authentication/forms");
                    _authentificationTimeout = (int.Parse(node.Attributes["timeout"].Value, System.Globalization.CultureInfo.InvariantCulture.NumberFormat) * 60000) - 30000;
                }
                catch
                {
                    _authentificationTimeout = 1770000;
                }

                return _authentificationTimeout.GetValueOrDefault(1770000);
            }
        }
	}
}